package com.vess.ratingweb;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream.PutField;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class AdminSettings extends Activity {

	private EditText mEdiSample;
	private Button mBtnSave;
	private ImageView mLogo;
	private int RESULT_LOAD_LOGO;
	private int RESULT_LOAD_SAMPLE;
	private String imagePath ="";
	private String imageSample ="";
	private ImageView mImgSample;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		mEdiSample = (EditText) findViewById(R.id.ediWelcome);
		mBtnSave = (Button) findViewById(R.id.btnSave);
		mLogo = (ImageView) findViewById(R.id.imgLogo);
		mImgSample = (ImageView) findViewById(R.id.imgSample);

		updateView();
		mLogo.setOnClickListener(new OnClickListener() {

			

			@Override
			public void onClick(View v) {
				RESULT_LOAD_LOGO = 1;

				Intent intent = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

				startActivityForResult(intent, RESULT_LOAD_LOGO);
			}
		});

		mImgSample.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				RESULT_LOAD_SAMPLE = 2;

				Intent intent = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

				startActivityForResult(intent, RESULT_LOAD_SAMPLE);
			}
		});
		
		mBtnSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent resultIntent = new Intent();
				resultIntent.putExtra("sampleText", mEdiSample.getText()
						.toString());
				Log.i("eventId", mEdiSample.getText().toString() + "");
				Log.i("imageSample", imageSample + "");
				Log.i("imagePath", imagePath + "");
				setResult(RESULT_OK, resultIntent);
				Attributes.setInfoSettings(imagePath, mEdiSample.getText()
						.toString(), imageSample);
				finish();
			}
		});

	}
	
	private void updateView() {
		Log.i("updateView", "passed");
		if (Attributes.mLogoPath.equals("") == false) {
//			imgLogo.setImageBitmap(resizeImage(Attributes.mLogoPath, imgLogo.getWidth(), imgLogo.getHeight()));
			mLogo.setImageURI(Uri.parse(Attributes.mLogoPath));
			imagePath = Attributes.mLogoPath;
			Log.i("Logo", "passed");
		}
		if (Attributes.mWelCoImg.equals("") == false) {
//			imgSample.setImageBitmap(resizeImage(Attributes.mWelCoImg, imgLogo.getWidth(), imgLogo.getHeight()));
			mImgSample.setImageURI(Uri.parse(Attributes.mWelCoImg));
			imageSample = Attributes.mWelCoImg;
			Log.i("WelCoImg", "passed");
		}
		mEdiSample.setText(Attributes.mWelCoText);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
			if (null != data && resultCode == RESULT_OK) {
				// lay repository cua image dc chon trong gallery, ta vao
				// DDMS/File Explorer/mnt/sdcard/ de push image vao
				Uri selectedImage = data.getData();
				Log.i("selectedImage", data.getData() + " ");

				// mang dong du lieu cua file image
				String[] filePathColumn = { MediaStore.Images.Media.DATA };
				Log.i("filePathColumn", MediaStore.Images.Media.DATA + " ");

				// su dung method getContentResolver cua context de truy van toi
				// repository voi dieu kien la dong data cua file image
				Cursor cursor = getContentResolver().query(selectedImage,
						filePathColumn, null, null, null);

				Log.i("namecolumnIndex", filePathColumn[0] + " ");

				cursor.moveToFirst();
				// lay index cua column voi ten trong dong du lieu
				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

				// lay duong dan cua file anh voi index column da tim dc
				if (requestCode == RESULT_LOAD_LOGO) {
					imagePath = cursor.getString(columnIndex);
					mLogo.setImageBitmap(resizeImage(imagePath));
					
				} else if (requestCode == RESULT_LOAD_SAMPLE) {
					imageSample = cursor.getString(columnIndex);
					mImgSample.setImageURI(Uri.parse(imageSample));
				}
				Log.i("picturePath", cursor.getString(columnIndex) + " ");
				cursor.close();

				Log.i("picturePath", selectedImage.getPath() + " ");

//				imgFrameView.setImageBitmap(resizeImage(imagePath));
//				imgFrameView.setVisibility(View.VISIBLE);
			} else {
				Toast.makeText(this, "Please choose an image",
						Toast.LENGTH_SHORT).show();
			}
	}
	public Bitmap resizeImage(String pathImage) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(pathImage, options);

		// Calculate inSampleSize
		options.inSampleSize = calculateInSampleSize(options,
				mLogo.getWidth(), mLogo.getHeight());

		// Decode bitmap with inSampleSize set
		options.inJustDecodeBounds = false;
		return BitmapFactory.decodeFile(pathImage, options);
	}

	public static int calculateInSampleSize(BitmapFactory.Options options,
			int reqWidth, int reqHeight) {
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {
			if (width > height) {
				inSampleSize = Math.round((float) height / (float) reqHeight);
			} else {
				inSampleSize = Math.round((float) width / (float) reqWidth);
			}
		}
		return inSampleSize;
	}
}
